<?php
/**
 * Created by PhpStorm.
 * User: medivh
 * Date: 15-6-11
 * Time: 上午10:12
 */
/**
 * @param $timeInt为时间戳
 */
function timeFormat($timeInt, $format = 'Y-m-d H:i:s')
{
    if (empty($timeInt) || !is_numeric($timeInt) || !$timeInt) {
        return '';
    }
    $d = time() - $timeInt;
    if ($d < 0) {
        return '';
    } else {
        if ($d < 60) {
            return $d . '秒前';
        } else {
            if ($d < 3600) {
                return floor($d / 60) . '分钟前';
            } else {
                if ($d < 86400) {
                    return floor($d / 3600) . '小时前';
                } else {
                    if ($d < 25920000) {//30天内
                        return floor($d / 86400) . '天前';
                    } else {
                        return date($format, $timeInt);
                    }
                }
            }
        }
    }
}

/**
 * @param $str要分割的标签字符串
 * @param $f制定分隔符 ，默认为%
 */
function label_arr($str, $f = '%')
{
    $arr = explode($f, $str);
    return $arr;
}

function label($label)
{
    $result = '';
    foreach (label_arr($label) as $key => $val) {
        $result .= "<span class='label label-default " . RandForLabel() . "' contenteditable='false'>" . $val . "</span> ";
    }
    return $result;
}

/**
 * @param $label标签样式
 */
function RandForLabel()
{
    $label = array(
        '1' => 'label-info',
        '2' => 'label-danger',
        '3' => 'label-success',
        '4' => 'label-warning',
        '5' => 'label-primary'
    );
    $a = array_rand($label);
    return $label[$a];
}

function RandForProgress()
{
    $label = array(
        '1' => 'bar-info',
        '2' => 'bar-danger',
        '3' => 'bar-success',
        '4' => 'bar-warning',
        '5' => 'bar-primary'
    );
    $a = array_rand($label);
    return $label[$a];
}

function RandTopBorder()
{
    $border = array(
        'red', 'blue', 'green', 'yellow', 'black',
        '#FFF68F', '#FFC1C1', '#FF7F00', '#FF8C69', '#FF4500',
        '#FF00FF', '#EE9A49', '#EE6A50', '#D15FEE', '#CD919E',
        '#BCEE68', '#B0E2FF', '#AB82FF', '#A020F0', '#8DEEEE',
        '#548B54', '#545454', '#53868B', '#528B8B', '#525252',
        '#515151', '#4F94CD', '#4F4F4F', '#4EEE94', '#4D4D4D',
        '#4B0082', '#4A708B', '#4A4A4A', '#48D1CC', '#4876FF',
        '#483D8B', '#474747', '#473C8B', '#4682B4', '#458B74',
        '#458B00', '#454545', '#43CD80', '#436EEE', '#424242',
        '#4169E1', '#40E0D0', '#404040', '#3D3D3D', '#3CB371',
        '#3B3B3B', '#3A5FCD', '#388E8E', '#383838', '#36648B',
        '#363636', '#333333', '#32CD32', '#303030', '#2F4F4F',
        '#2E8B57', '#2E2E2E', '#2B2B2B', '#292929', '#282828',
        '#27408B', '#262626', '#242424', '#228B22', '#218868',
        '#212121', '#20B2AA', '#1F1F1F', '#1E90FF', '#1E1E1E',
        '#1C86EE', '#1C1C1C', '#1A1A1A', '#191970', '#1874CD',
        '#171717', '#141414', '#121212', '#104E8B', '#0F0F0F',
        '#0D0D0D', '#0A0A0A', '#080808', '#050505', '#030303',
        '#00FFFF', '#00FF7F', '#00FF00', '#00FA9A', '#00F5FF',
        '#00EEEE', '#00EE76', '#00EE00', '#00E5EE', '#00CED1',
        '#00CDCD', '#00CD66', '#00CD00'

    );
    $a = array_rand($border);
    return $border[$a];
}

function strCut($str, $length)
{
    $str = trim($str);
    $string = "";
    if (strlen($str) > $length) {
        for ($i = 0; $i < $length; $i++) {
            if (ord($str) > 127) {
                $string .= $str[$i] . $str[$i + 1] . $str[$i + 2];
                $i = $i + 2;
            } else {
                $string .= $str[$i];
            }
        }
        $string .= "...";
        return $string;
    }
    return $str;
}

function NewsType($type)
{
    $reslut = M('news_column')->where(array('id' => $type))->find();
    return $reslut['title'];
}

function dig($digital)
{
    $result = '';
    $result = $digital * 3 - rand(1, 2);
    $result = $result < 1 ? $digital * 3 : $result;
    return $result;
}

function GetRand($proArr)
{
    $result = '';
    //概率数组的总概率精度
    $proSum = array_sum($proArr);
    //概率数组循环
    foreach ($proArr as $key => $proCur) {
        $randNum = mt_rand(1, $proSum);             //抽取随机数
        if ($randNum <= $proCur) {
            $result = $key;                         //得出结果
            break;
        } else {
            $proSum -= $proCur;
        }
    }
    unset ($proArr);
    return $result;
}

function reply($id)
{
    $data = array(
        'id' => $id
    );
    $content = M('news_comment')->where($data)->find();
    if (count($content) != 0) {
        $list = "<div class='replay col-xs-12'>
                    <label class='replay-name'>" . $content['nickname'] . "</label>
                    <p class='replay-p1'>
                    <span class='replay-time'>" . timeFormat($content['createtime']) . "</span>
                    <span class='replay-from'><span class='glyphicon glyphicon-map-marker'></span> 来自" . $content['province'] . "</span>
                    </p>
                    <p class='replay-content'>" . $content['content'] . "</p>
                    </div>";
        echo $list;
    } else {
        echo '<p style="width: 100%;height:2px;margin:0;padding:0"></p>';
    }
}

function LastTime($id)
{
    $arr = array(
        'tid' => $id
    );
    $LastTime = M('news_comment')->where($arr)->order('createtime desc')->limit('0,1')->find();
    if (!$LastTime['createtime']) {
        $time = M('news_details')->where(array('id' => $id))->find();
        return timeFormat($time['createtime']);
    } else {
        return timeFormat($LastTime['createtime']);
    }
}

//$arr这里指一个二维数组，一个用户签到的历史时间数组。KEY为sign_time
function  sign_continue($arr)
{

    $i = 0;
    foreach ($arr as $key => $value) {
        $day0 = "-" . $key . "days";
        $key1 = $key - 1;
        $day1 = "-" . $key1 . "days";


        $daytime0 = strtotime(date('Y-m-d', strtotime($day0)));
        $daytime1 = strtotime(date('Y-m-d', strtotime($day1)));


        if ($value["sign_time"] >= $daytime0 && $value["sign_time"] <= $daytime1) {


            $i++;
            continue;
        } else {
            break;
        }


    }


    return $i;


}

function IndexLastTime()
{
}
//发送红包直接调用的函数。$openid,$bonus

function old_cut($user_name)
{


 $strlen = mb_strlen($user_name, 'utf-8');
    $firstStr = mb_substr($user_name, 0, 1, 'utf-8');
    $lastStr = mb_substr($user_name, -1, 1, 'utf-8');
    if($strlen == 2){
        $strlen = $firstStr . "   " . $lastStr;
    }elseif($strlen == 1){
        $strlen = $user_name."      ";
    }else{
        $strlen = $firstStr . '**' . $lastStr;
    }
    return $strlen;






}


function substr_cut($user_name)
{
    $strlen = mb_strlen($user_name, 'utf-8');
    $firstStr = mb_substr($user_name, 0, 6, 'utf-8');
   
    if($strlen == 2){
        $strlen = $firstStr . "   " . $lastStr;
    }elseif($strlen == 1){
        $strlen = $user_name."      ";
    }
    elseif($strlen<=0)
    {

  $strlen="*   *";


    }
    elseif($strlen>6)
    {

  $strlen=$firstStr."*";


    }


    else{
        $strlen = $firstStr;
    }
    return $strlen;

}
function sendbonus($openid,$bonus)
{

$pay=new \DaChongQing\Controller\BonusController($openid,$bonus);

                    $md5sign = $pay->SetSign();
                    $parameters = array(
                        'nonce_str' => '123123131231321',
                        'sign' => $md5sign,
                        'mch_billno' => $pay->wxMchBillno('1252370801'),
                        'mch_id' => C('MCH_ID'),
                        'wxappid' => 'wx22c589512a2d5f61',
                        'nick_name' => '我爱大重庆',
                        'send_name' => '我爱大重庆',
                        're_openid' => $openid,
                        'total_amount' => $bonus,
                        'min_value' => $bonus,
                        'max_value' => $bonus,
                        'total_num' => '1',
                        'wishing' =>'恭喜发财！',
                        'client_ip' => '121.40.30.107',
                        'act_name' => '恭喜发财',
                        'remark' => '恭喜发财'
                    );
                    $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack';
                    return $pay->wxHttpsRequestPem($url,$pay->wxArrayToXml($parameters));








}